<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
          :model="where"
          label-width="77px"
          class="ele-form-search"
          @keyup.enter.native="reload"
          @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="卡名称:">
              <el-input
                  clearable
                  v-model="where.name"
                  placeholder="请输入卡名称"/>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="6">
            <el-form-item label="用户手机号:">
              <el-input
                  clearable
                  v-model="where.mobile"
                  placeholder="请输入用户手机号"/>
            </el-form-item>
          </el-col>
          <el-col :lg="3" :md="6">
            <el-form-item label="兑换日期:" prop="add_time">
              <el-date-picker
                  type="date"
                  clearable
                  v-model="where.add_time"
                  placeholder="请输入兑换开始时间"/>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="6">
            <el-form-item label="兑换日期:" prop="end_time">
              <el-date-picker
                  type="date"
                  clearable
                  v-model="where.end_time"
                  placeholder="请输入兑换结束时间"/>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="6">
            <div class="ele-form-actions">
              <el-button
                  type="primary"
                  icon="el-icon-search"
                  class="ele-btn-icon"
                  @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
          ref="table"
          :where="where"
          :datasource="url"
          :columns="columns"
          :selection.sync="selection"
          height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">

          <el-button
              size="small"
              type="danger"
              icon="el-icon-delete"
              class="ele-btn-icon"
              @click="removeBatch"
              v-if="permission.includes('sys:meicrad:dall')">删除
          </el-button>
          <el-button
              size="small"
              type="success"
              icon="el-icon-download"
              class="ele-btn-icon"
              @click="exportExcel"
              v-if="permission.includes('sys:meicrad:export')">导出
          </el-button>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">

          <el-popconfirm
              class="ele-action"
              title="确定要删除此用户期限卡吗？"
              @confirm="remove(row)">
            <el-link
                type="danger"
                slot="reference"
                :underline="false"
                icon="el-icon-delete"
                v-if="permission.includes('sys:meicrad:delete')">删除
            </el-link>
          </el-popconfirm>
        </template>

        <template slot="pay_status" slot-scope="{row}">
          <el-tag v-if="row.pay_status === 1" type="success" size="small">兑换成功</el-tag>
          <el-tag v-if="row.pay_status === 0" type="danger" size="small">兑换失败</el-tag>
          <el-tag v-if="row.pay_status === 3" type="warning" size="small">已退款</el-tag>
        </template>
        <template slot="pay_type" slot-scope="{row}">
          <el-tag v-if="row.pay_type === 5" type="danger" size="small">美团兑换</el-tag>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->

  </div>
</template>

<script>
import {mapGetters} from "vuex";


export default {
  name: 'ExamUsercrad',
components: {OrderDetails,Delivery,Refunds},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/meicrad/index',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: 'mobile',
          label: '用户手机号',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },

        {
          prop: 'name',
          label: '卡名称',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'type_name',
          label: '卡类型',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'class_name',
          label: '分类名称',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'pay_status',
          label: '是否支付',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
          slot: 'pay_status',
        },



        {
          prop: 'price',
          label: '价格',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'pay_price',
          label: '真实支付金额',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },

        {
          prop: 'amount',
          label: '卡额',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },



        {
          prop: 'add_time',
          label: '开始时间',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'end_time',
          label: '过期时间',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },





        {
          prop: 'pay_type',
          label: '支付类型',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100,
          slot: 'pay_type',
        },

        {
          prop: 'notes',
          label: '备注',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'create_time',
          label: '兑换时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },

        {
          columnKey: 'action',
          label: '操作',
          width: 150,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      showEdits: false,
    };
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {

      this.current = row;
      this.showEdit = true;
    },

    /* 显示编辑 */
    openEdits(row) {

      this.current = row;
      this.showEdits = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/meicrad/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的用户期限卡吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/meicrad/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
         /* 导出数据Excel */
         exportExcel() {
          let info = JSON.parse(JSON.stringify(this.where));
          this.$http
              .get("/Order/exportExcel", info)
              .then((res) => {
                let data = res.data;
                if (data.code == 0) {
                  // 下载文件
                  window.location.href = data.data;
                  this.$message({
                    type: "success",
                    message: "导出成功",
                  });
                } else {
                  this.$message.error("导出失败");
                }
              })
        },

  }
}
</script>

<style scoped>
</style>
